package com.jysz.certificate.web.action.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.model.core.Attachment;
import com.jysz.certificate.service.core.AttachmentService;
import com.jysz.certificate.utils.CommonUtil;

/**
 * 释放单
 * @author xuxiaolong
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/attach")
@Results({@Result( type = "stream", params = {  
        "contentType", "application/octet-stream",  
        "inputName", "inputStream", "contentDisposition",  
        "attachment;filename=\"${downloadFileName}\"", "bufferSize",  
        "4096" })}) 
public class AttachmentAction extends AbstractBaseAction {
	private static final long serialVersionUID = 6775153817953371737L;

	@Resource
	private AttachmentService service;
	
	 private String id;//附件id  
	    private String fileName;// 初始的通过param指定的文件名属性  
	    private Attachment attachment;//附件对象  
	   
	    @Action("download")
	    public String download() throws Exception {  
	        if(CommonUtil.isNotEmpty(id)){
	        	attachment=service.find(Long.parseLong(id));
	            setFileName(attachment.getName());  //
	        }  
	        return SUCCESS;  
	    }
	    
	    @Action("del")
	    public String del() throws Exception {  
	        if(CommonUtil.isNotEmpty(id)){
	        	setJsonMsg("", service.delFile(getUser(), Long.parseLong(id)), null);
	        }  
	        return JSON_PAGE;  
	    }
	  
	    public InputStream getInputStream() throws Exception {  
	    	
	        return new FileInputStream(new File(attachment.getPath()));  
	    }  
	  
	    public String getDownloadFileName() {  
	        return fileName;  
	    }  
	  
	    public void setFileName(String fileName) throws UnsupportedEncodingException {  
	        String agent = ServletActionContext. getRequest().getHeader("User-agent");  
	        //如果浏览器是IE浏览器，就得进行编码转换  
	        if(agent.contains("MSIE")){  
	            this.fileName = URLEncoder.encode(fileName, "UTF-8");  
	        }else{  
	            this.fileName = new String(fileName.getBytes(),"ISO-8859-1");    
	        }  
	    }

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public Attachment getAttachment() {
			return attachment;
		}

		public void setAttachment(Attachment attachment) {
			this.attachment = attachment;
		}

		public String getFileName() {
			return fileName;
		}  
}
